package com.gitee.hermer.engine.jars.dependent.dozer.configuration;

import java.io.IOException;
import java.util.Map;

import javax.sql.DataSource;

import org.dozer.spring.DozerBeanMapperFactoryBean;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.AnnotationMetadata;

import com.gitee.hermer.boot.jee.commons.bean.utils.BeanLocator;
import com.gitee.hermer.boot.jee.commons.collection.CollectionUtils;
import com.gitee.hermer.boot.jee.commons.log.UtilsContext;
import com.gitee.hermer.engine.jars.core.domain.EngineConstant;
import com.gitee.hermer.engine.jars.dependent.dozer.annotate.EnableDozer;



@Configuration
public class DozerAutoConfiguration extends UtilsContext implements Ordered,ImportAware{

	private AnnotationAttributes metadataAttributes;
	
	@Override
	public void setImportMetadata(AnnotationMetadata importMetadata) {
		if(importMetadata != null){
			Map<String, Object> attributes = importMetadata.getAnnotationAttributes(EnableDozer.class.getName(), false);
			this.metadataAttributes = AnnotationAttributes.fromMap(attributes);
		}
		info("AnnotationMetadata -> [%s]",this.metadataAttributes.toString());
	}
	
	@Bean
	public DozerBeanMapperFactoryBean mapper()
			throws IOException
	{
		if(metadataAttributes == null)
			throw new BeanCreationException("not find @EnableDozer attributes");
		DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean();
		PathMatchingResourcePatternResolver s = new PathMatchingResourcePatternResolver();
		dozerBeanMapperFactoryBean.setMappingFiles(s.getResources(metadataAttributes.getString(EngineConstant.ANNOTATE_FIELD_NAME_VALUE)));
		return dozerBeanMapperFactoryBean;
	}

	@Override
	public int getOrder() {
		return 0;
	}






}
